package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApCollectionBehaviorService;
import com.heima.commom.exception.CustException;
import com.heima.common.constants.article.HotArticleConstants;
import com.heima.model.behavior.dtos.CollectionBehaviorDTO;
import com.heima.model.behavior.pojo.ApBehaviorEntry;
import com.heima.model.behavior.pojo.ApCollection;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.mess.app.NewBehaviorDTO;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojo.ApUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author by jojo
 * @Date 2022/3/8
 * @Description
 */
@Service
@Slf4j
public class ApCollectionBehaviorServiceImpl implements ApCollectionBehaviorService {
    @Autowired
    private ApBehaviorEntryService apBehaviorEntryService;
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 收藏 取消收藏
     * @param dto
     * @return
     */
    @Override
    public ResponseResult collectBehavior(CollectionBehaviorDTO dto) {
//       1. 校验参数 需要登录 文章id不能null  操作类型 0 或 1
        //是否登录
        ApUser apUser = AppThreadLocalUtils.getUser();
        if (apUser == null) {
            log.error("需要登录后才可以操作不喜欢行为！！！");
            CustException.cust(AppHttpCodeEnum.NEED_LOGIN);
        }
        Integer equipmentId = dto.getEquipmentId();
        Short operation = dto.getOperation();
        //       2. 根据userId查询行为实体数据
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(apUser.getId(), equipmentId);
        String entryId = apBehaviorEntry.getId();
        Long articleId = dto.getArticleId();
//       3. 如果是收藏操作 判断是否已经收藏
        ApCollection one = null;
        Query query = Query.query(Criteria.where("entryId").is(entryId).and("articleId").is(articleId));
        if (operation.equals(ApCollection.Type.ARTICLE.getCode())) {
             one = mongoTemplate.findOne(query, ApCollection.class);
            if (one==null) {
                // 4. 如果未收藏 新增收藏行为
                ApCollection apCollection = new ApCollection();
                apCollection.setEntryId(entryId);
                apCollection.setArticleId(articleId);
                apCollection.setType(ApCollection.Type.ARTICLE.getCode());
                apCollection.setCollectionTime(new Date());
                mongoTemplate.save(apCollection);
            }
        }else {
            // 5.如果是取消收藏操作  删除收藏行为
            mongoTemplate.remove(query,ApCollection.class);
        }
        NewBehaviorDTO messDto = new NewBehaviorDTO();
        messDto.setType(NewBehaviorDTO.BehaviorType.COLLECTION);
        messDto.setArticleId(dto.getArticleId());
        messDto.setAdd(1);
        rabbitTemplate.convertAndSend(HotArticleConstants.HOT_ARTICLE_SCORE_BEHAVIOR_QUEUE, JSON.toJSONString(messDto));
        log.info("收藏行为发送MQ消息成功  ==》参数={}",messDto);
        return ResponseResult.okResult();
    }
}
